/**
 * 数据源转化类
 */
import { generatorFunctionWithString, execFunctionWithException } from "../utils"
import ObtainData from "./obtainData";
interface IConvertDataType {
  origin: string;  // 数据源模式. "static" | "upload" | "request"
  convertFunc?: string;  // 数据转化方法的函数体
  [key: string]: any;  // 对应数据源模式的其他参数
}

class DataConvert extends ObtainData {
  async convertData(params: IConvertDataType) {
    let convertFunction = params.convertFunc && generatorFunctionWithString(params.convertFunc, "data");
    let baseData;
    switch (params.origin) {
      case "static":
      case "upload":
        baseData = params.data;
        break;
      case "request":
        baseData = await this.getRequestData((params as any));
        break;
      default:
        break;
    }
    return convertFunction ? execFunctionWithException(convertFunction, baseData, baseData) : baseData;
  }
}

export default new DataConvert();